package cn.ztuo.bitrade.controller;

import cn.ztuo.bitrade.entity.ContractBond;
import cn.ztuo.bitrade.service.ContractBondService;
import cn.ztuo.bitrade.service.ContractOrderService;
import cn.ztuo.bitrade.util.ContractUtils;
import cn.ztuo.bitrade.util.MessageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取合约所有的杠杆
 */
@RequestMapping("/contract-bond")
@RestController
public class ContractBondController {
    @Autowired
    private ContractBondService bondService;
    @Autowired
    private ContractOrderService contractOrderService;


    @PostMapping("/getlistlocation")
    public  MessageResult calculation(String symbol){
        MessageResult mr = new MessageResult(0,"success");
        Sort sort = new Sort(Sort.Direction.DESC,"multiple");
        List<ContractBond> byAll =  bondService.findBySymbol(symbol,sort);
        byAll=byAll.stream().sorted(Comparator.comparing(a->Integer.parseInt(a.getMultiple()))).collect(Collectors.toList());
        mr.setData(byAll);
        return mr;
    }

    /**
     * 根据杠杆 得到对应的保证金
     * @param symbol
     * @param lever
     * @param amount
     * @param price
     * @return
     */
    @PostMapping("/getlever")
    public MessageResult getlever(String symbol, BigDecimal lever, BigDecimal amount, BigDecimal price){
        MessageResult mr = new MessageResult(0,"success");
        ContractBond bySymbolAndMultiple = bondService.findBySymbolAndMultiple(symbol,lever);
        BigDecimal initialMargin = bySymbolAndMultiple.getInitialMargin();
        BigDecimal getbond = ContractUtils.getbond(amount, price, initialMargin);
        mr.setData(getbond);
        return mr;
    }
}
